// pages/index/acrossAd/acrossAd.js
import { list as wxPageList } from '../../../../api/wxPage'
const util = require('../../../../utils/util')
import { list as navigationList } from '../../../../api/navigation'
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
    }
  },
  lifetimes: {
    attached() {
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })

      this.loadPage()
      this.loadnavigation()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    navigations: [],
    tagIds: [],
    pageList: [],
    pageNum: 1,
    pageSize: 3,
    haveThumbnial: false
  },

  created() {
  },
  ready: function (e) {

  },

  /**
   * 组件的方法列表
   */

  methods: {
    loadnavigation() {
            let _this = this
      if (this.data.tagIds.length > 0) {
        navigationList({
          mchId: app.globalData.mchId,
          tagIds: this.data.tagIds
        }).then(res => {
        //   console.info("精选榜单的数据navigations--")
        //   console.info(res)
          this.data.navigations = res.data.data
          if (this.data.navigations.length>=3)  {
                this.data.navigations[0].logo = util.thumbnail(_this.data.navigations[0].logo, 334, 334)
                this.data.navigations[1].logo = util.thumbnail(_this.data.navigations[1].logo, 334, 167)
                this.data.navigations[2].logo = util.thumbnail(_this.data.navigations[2].logo, 334, 167)
              }
                // console.info(this.data.navigations)
          this.setData({
            navigations: this.data.navigations
          })
        })
      } else {
        navigationList({
          mchId: app.globalData.mchId,
        }).then(res => {
          this.data.navigations = res.data.data
          this.setData({
            navigations: this.data.navigations
          })
        })
      }
    },

    toLink(e) {
      console.log(e)

      if (e.currentTarget.dataset.id.type == 1) {
        wx.navigateTo({
          url: '/pages/goods/category/category?id=' + e.currentTarget.dataset.id.typeId
        })
      }
      if (e.currentTarget.dataset.id.type == 2) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id.typeId
        })
      }
      if (e.currentTarget.dataset.id.type == 3) {
        wx.navigateTo({
          url: '/pages/goods/product/details?id=' + e.currentTarget.dataset.id.typeId,
        })
      }
    },

    topage(e) {
      wx.navigateTo({
        url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id
      })

    },

    loadPage() {
      var _this = this;
      wxPageList({
        mchId: app.globalData.mchId,
        tagIds: this.data.tagIds,
      }).then(res => {
        console.info("精选榜单的数据pageList--")
        console.info(res)
        _this.data.pageList = res.data.data

        let haveThumbnial = this.data.haveThumbnial
        if (_this.data.pageList.length > 0) {
          if (!util.isNull(_this.data.pageList[0].thumbnial)) {
            _this.data.pageList[0].thumbnial = util.thumbnail(_this.data.pageList[0].thumbnial, 710, 446)
            haveThumbnial = true
          }

        }
        _this.setData({
          pageList: _this.data.pageList,
          haveThumbnial: haveThumbnial
        })

      })
    },
  }
})
